package com.dt.app.controller.customer;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.time.DateFormatUtils;
import javax.validation.Valid;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.servlet.ModelAndView;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;
import com.dt.app.common.response.ResultUtil;
import com.dt.app.modules.customer.entities.CustomerNeed;
import com.dt.app.modules.customer.vo.CustomerNeedBean;
import com.dt.app.api.customer.service.ICustomerNeedService;
import com.dt.app.controller.base.BaseController;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 * @author 陈辉
 * @since 2024-03-13
 */
@RestController
@RequestMapping("api/v1/customer/customerNeed")
public class CustomerNeedController extends BaseController{
    @Autowired
    private ICustomerNeedService customerNeedService;

    @ApiOperation(value = "列表")
    @RequestMapping("list")
    public ResultUtil<IPage<CustomerNeedBean>> list(CustomerNeedBean request){
        QueryWrapper<CustomerNeed> wrapper = new QueryWrapper<>();
        if(null != request.getStartTime() && null != request.getEndTime()) {
            wrapper.between("create_time",DateFormatUtils.format(request.getStartTime(),"yyyy-MM-dd HH:mm:ss"),DateFormatUtils.format(request.getEndTime(),"yyyy-MM-dd HH:mm:ss"));
        }
        wrapper.orderByDesc("${keyTableName}");
        Page page = new Page<>(request.getCurrent(),request.getSize());
        IPage listBody = customerNeedService.page(page,wrapper);
        List<CustomerNeed> entityList=listBody.getRecords();
        if(!entityList.isEmpty()) {
            List<CustomerNeedBean> responseList = new ArrayList<>();
            entityList.forEach(it -> {
                CustomerNeedBean bean = new CustomerNeedBean();
                BeanUtils.copyProperties(it,bean);
                responseList.add(bean);
            });
            listBody.setRecords(responseList);
        }
        return ResultUtil.success(listBody);
    }

    @PostMapping("save")
    @ApiOperation(value = "新增")
    public ResultUtil save(@RequestBody @Valid CustomerNeed entity){
        //entity.setCreateTime(LocalDateTime.now());entity.setDeleted(0);
        entity.setDeleted(0);
        customerNeedService.save(entity);
        return ResultUtil.success();
    }

    @GetMapping("getById/{id}")
    @ApiOperation(value = "id查询")
    public ResultUtil<CustomerNeedBean> getById(@PathVariable String id){
        CustomerNeed entity = customerNeedService.getById(id);
        if(entity!=null){
            CustomerNeedBean bean = new CustomerNeedBean();
            BeanUtils.copyProperties(entity,bean);
            return ResultUtil.success(bean);
        }
        return ResultUtil.error("没有找到对象");
    }

    @PutMapping("update")
    @ApiOperation(value = "修改")
    public ResultUtil update(@RequestBody @Valid CustomerNeed entity){
        customerNeedService.updateById(entity);
        return ResultUtil.success();
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("delete/{id}")
    public ResultUtil delete(@PathVariable Long id){
        customerNeedService.removeById(id);
        return ResultUtil.success();
    }
}